import scrapy
import re
from esc.items import EscItem
def checkdata(mylist):
    i = 0
    while i < len(mylist):
        list[i] += "万"
        i += 1


class ErshoucheSpider(scrapy.Spider):
    name = 'ershouche'
    allowed_domains = ['che168.com']
    start_urls = ['https://www.che168.com/china/bieke/a0_0msdgscncgpi1lto8csp1exx0/']
    # 根据规律手动获取url
    for link in range(2, 101):
        start_urls.append('https://www.che168.com/china/bieke/a0_0msdgscncgpi1lto8csp{lin}exx0/'.format(lin=link))

    def start_requests(self):
        for url in self.start_urls:
            print(url)
            yield scrapy.Request(
                url=url,
                callback=self.parse
            )

    def parse(self, response):
        li = response.xpath('/html/body/div[12]/div[1]/ul')
        i = 0
        flag = 0
        car = EscItem()

        for get_li in li:
            title = get_li.xpath("./li/a/div/h4/text()").extract()
            msg = get_li.xpath("./li/a/div/p/text()").extract()
            nowpri = get_li.xpath("./li/a/div/div/span/em/text()").extract()
            newpri = get_li.xpath("./li/a/div/div/s/text()").extract()
            # 处理数据
            while i < len(nowpri):
                nowpri[i] += '万'
                i += 1
            for j in range(len(title)):
                try:
                    car["title"] = title[j]
                    car["msg"] = msg[j]
                    car["nowpri"] = nowpri[j]
                    car["newpri"] = newpri[j]
                    flag += 1
                    print(title[j], msg[j], nowpri[j], newpri[j], "次数", flag)
                    yield car
                except Exception:
                    print("异常:", Exception.args)

    def parse_detail(self, resp, **kwargs):
        pass
